import Vue from 'vue'
import App from './App.vue'
import router from './router'
import Vuex from 'vuex'
import store from './store/index'
import './plugins/element.js'
import './assets/css/global.css'

// 导入字体图标
import './assets/font/iconfont.css'
import TreeTable from 'vue-table-with-tree-grid'
import axios from 'axios'

// 导入富文本编辑器
import VueQuillEditor from 'vue-quill-editor'
import 'quill/dist/quill.core.css' // import styles
import 'quill/dist/quill.snow.css' // for snow theme
import 'quill/dist/quill.bubble.css' // for bubble theme

// 自定义表格工具扩展
import RightToolbar from './components/RightToolbar/index.vue'

// 导入进度条
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

axios.defaults.baseURL = 'http://127.0.0.1:8001'
// axios.defaults.baseURL = 'http://172.18.42.210:8001'
axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'
axios.defaults.timeout = 5000
// 在request 拦截器中,展示进度条 NProgress.start()
axios.interceptors.request.use(config => {
    NProgress.start()
    // 给所有请求授权，就是添加令牌
    config.headers.Authorization = window.sessionStorage.getItem('token')
    return config
})
// response 拦截器中,隐藏进度条 NProgress
axios.interceptors.response.use(config => {
    NProgress.done()
    return config
},error=>{
    NProgress.done()
    let code = 0
    try {     
      code = error.response.data.status
    } catch (e) {
       //做错误判断
      if (error.toString().indexOf('Error: timeout') !== -1) {        
        Vue.prototype.$message.error('网络请求超时') 
        return Promise.reject(error)
      }
      if (error.toString().indexOf('Error: Network Error') !== -1) {        
        Vue.prototype.$message.error('网络请求错误')        
        return Promise.reject(error)
      }
    }
    if (code === 401) {       
        Vue.prototype.$message.error('登录状态已过期，您可以继续留在该页面，或者重新登录')
    } else if (code === 403) {
        router.push({ path: '/401' })
    } else {
        const errorMsg = error.response.data.message
        if (errorMsg !== undefined) {
            Vue.prototype.$message.error(errorMsg)
        }
    }
    return Promise.reject(error)
})
Vue.use(Vuex)
Vue.prototype.$http = axios
Vue.config.productionTip = false
Vue.component('tree-table', TreeTable)
Vue.component('RightToolbar', RightToolbar)
// Vue.component('svg-icon', SvgIcon)
// 将富文本编辑器,注册为全局可用的组件
Vue.use(VueQuillEditor)


Vue.filter('dateFormat', function (originVal) {
    const dt = new Date(originVal)
    const y = dt.getFullYear()
    const m = (dt.getMonth() + 1 + '').padStart(2, '0')
    const d = (dt.getDay() + '').padStart(2, '0')
    const hh = (dt.getHours() + '').padStart(2, '0')
    const mm = (dt.getMinutes() + '').padStart(2, '0')
    const ss = (dt.getSeconds() + '').padStart(2, '0')
    return `${y}-${m}-${d} ${hh}:${mm}:${ss}`
})

new Vue({
    router,
    store,
    render: h => h(App)
}).$mount('#app')
